import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '../store/index';
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/index',
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('../views/home/index.vue'),
        meta: {
          title: '首页'
        },
      },
      {
        path: 'cate',
        name: 'cate',
        component: () => import('../views/cate/cate.vue'),
        meta: {
          title: '分类'
        }
      },
      {
        path: 'cart',
        name: 'cart',
        component: () => import('../views/cart/cart.vue'),
        meta: {
          title: '购物车'
        },
        beforeEnter: (to, from, next) => {
          if (store.state.token == '') {
            next('/login')
          } else {
            next()
          }
        },
      },
      {
        path: 'my',
        name: 'my',
        component: () => import('../views/my/my.vue'),
        meta: {
          title: '个人中心'
        },
        beforeEnter: (to, from, next) => {
          if (store.state.token == '') {
            next('/login')
          } else {
            next()
          }
        },
      },
    ]
  },
  {
    path: '/discount',
    name: 'discount',
    component: () => import('../views/home/discount.vue'),
  },
  {
    path: '/list',
    name: 'list',
    component: () => import('../views/home/list.vue'),
  },
  {
    path: '/theamlist',
    name: 'theamlist',
    component: () => import('../views/home/theamlist.vue'),
  },
  {
    path: '/hotlist',
    name: 'hotlist',
    component: () => import('../views/home/hotlist.vue'),
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/cate/search.vue'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/my/login.vue'),
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('../views/cate/detail.vue'),
  },
]

const router = new VueRouter({
  routes
})

export default router
